package com.pn.config;

import com.pn.filter.LoginCheckFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;


/**
 * @Author : LH
 * @create 2023/12/10 22:21
 */
/*
原生servlet配置类
 */
@Configuration
public class ServletConfig {
	//注入redis模板
	@Autowired
	private StringRedisTemplate redisTemplate;
	
	/**
	 * 注册原生Servlet的Filter
	 */
	@Bean
	public FilterRegistrationBean securityFilter(){
		
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		//创建SecurityFilter对象
		LoginCheckFilter loginCheckFilter = new LoginCheckFilter();
		//给SecurityFilter对象注入redis模板
		loginCheckFilter.setRedisTemplate(redisTemplate);
		//注册SecurityFilter
		filterRegistrationBean.setFilter(loginCheckFilter);
		//配置SecurityFilter拦截所有请求
		filterRegistrationBean.addUrlPatterns("/*");
		
		return filterRegistrationBean;
	}
}
